/**
 * 
 */
package com.code.google.p.jyve;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 * @author Informatica
 *
 */
public class ImagePanel extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 9159511803264052576L;

	private String mFileName;
	
	private Image mImage;
	
	
	
	public ImagePanel() {
	}
	
	@Override
	public void paintComponent(Graphics g) {
		if (mImage != null)	g.drawImage(mImage, 0, 0, getWidth(), getHeight(), null);
	}
	
	public void setImage(String filename){
		mFileName = filename;
		loadImage();
	}
	
	public void setImage(BufferedImage img){
		mImage = img;
	}
	
	public void setImageFromClasspathResource(String path){
		mImage = Toolkit.getDefaultToolkit().getImage(Window.class.getResource(path));
		mImage = mImage.getScaledInstance(getWidth(), getHeight(), Image.SCALE_SMOOTH);
	}
	
	private void loadImage(){
		try {
			mImage = ImageIO.read(new File(mFileName));
			mImage = mImage.getScaledInstance(getWidth(), getHeight(), Image.SCALE_SMOOTH);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
